package net.xxcxcxcx.xc_enhancedmob.component;

import net.minecraft.entity.EntityData;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.world.LocalDifficulty;
import net.minecraft.world.ServerWorldAccess;
import net.xxcxcxcx.xc_enhancedmob.data.MobData;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.column.sever.SeverCustomBossBarColumn;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.complete.CustomBossBarComplete;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.complete.sever.SeverCustomBossBarComplete;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.CustomBossBar;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.sever.SeverCustomBossBar;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.type.cover.TestBossBarCoverType;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.type.reality.TestClientCustomBossBar;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

public class BossBarComponent implements BaseComponent {
    public CustomBossBarComplete bossBarComplete = null;
    public CustomBossBar bossBar = null;
    public Text HealthName = new LiteralText("血量");
    public UUID HealthUUID = UUID.fromString("b258f1ef-1f11-d072-ac42-a62477e2eafc");
    public Text MainPart = new LiteralText("身躯");
    public UUID MainPartUUID = UUID.fromString("1eb8cd17-3bfd-1e9f-0110-23f121771230");

    public MobEntity mobEntity;

    public BossBarComponent(MobEntity mobEntity) {
        this.mobEntity = mobEntity;
    }

    public boolean HaveBossBar() {
        return mobEntity.world != null && !mobEntity.world.isClient &&
                ((MobData) mobEntity).getComponent(EnhancedBaseComponent.class.getName()) instanceof EnhancedBaseComponent component &&
                component.getRarity().ordinal() > 0;
    }

    @Override
    public void writeCustomDataToNbt(NbtCompound nbt) {
        if (bossBarComplete != null) {
            NbtCompound BossBarData = new NbtCompound();
            bossBarComplete.writeCustomDataToNbt(BossBarData);
            nbt.put("BossBar", BossBarData);
        }
    }

    @Override
    public void readCustomDataFromNbt(NbtCompound nbt) {
        if (nbt.contains("BossBar")) {
            NbtCompound BossBarData = nbt.getCompound("BossBar");
            bossBarComplete = new SeverCustomBossBarComplete(BossBarData);
        }
    }

    //创建一个新的bossbar
    public void CreatbossBar() {
        SeverCustomBossBarColumn bossBarColumn = new SeverCustomBossBarColumn(MainPartUUID, MainPart);
        bossBarColumn.addBossBar(new SeverCustomBossBar(
                HealthUUID,
                HealthName,
                new TestClientCustomBossBar(),
                mobEntity.getMaxHealth(),
                mobEntity.getHealth(),
                10,
                true,
                new TestBossBarCoverType()
        ));
        bossBarComplete = new SeverCustomBossBarComplete(mobEntity, mobEntity.getCustomName(), true);
        bossBarComplete.addBossColumn(bossBarColumn);
    }

    @Override
    public EntityData initialize(ServerWorldAccess world, LocalDifficulty difficulty, SpawnReason spawnReason, @Nullable EntityData entityData, @Nullable NbtCompound entityNbt) {
        if (HaveBossBar()) {
            CreatbossBar();
        }
        return null;
    }

    //当玩家追踪这个实体时候添加玩家信息给BossBar
    public void onStartedTrackingBy(ServerPlayerEntity player) {
        if (bossBarComplete instanceof SeverCustomBossBarComplete severbossbar) {
            severbossbar.addPlayer(player);
        }
    }

    //当玩家不追踪这个实体时候从BossBar移除玩家信息
    public void onStoppedTrackingBy(ServerPlayerEntity player) {
        if (bossBarComplete instanceof SeverCustomBossBarComplete severbossbar) {
            severbossbar.removePlayer(player);
        }
    }

    @Override
    public void tick() {
        if (bossBarComplete instanceof SeverCustomBossBarComplete severbossbar) {
            if (severbossbar.getName().getValue() == null || !severbossbar.getName().getValue().equals(mobEntity.getName())) {
                severbossbar.setName(mobEntity.getName());
            }
            if (bossBar == null) bossBar = severbossbar.getBossBar(HealthUUID);
            if (bossBar != null) {
                bossBar.setNowCount(mobEntity.getHealth());
                bossBar.setMaxCount(mobEntity.getMaxHealth());
            }
            bossBarComplete.tick(mobEntity);
        }
    }
}
